//
//  MGTableViewSection.m
//  Mango
//
//  Created by 胡超 on 2018/4/19.
//  Copyright © 2018年 胡超., ltd. All rights reserved.
//

#import "MGTableViewSection.h"
#import "MGTableView.h"

@interface MGTableViewSection ()
@end

@implementation MGTableViewSection

- (instancetype)init {
    if (self = [super init]) {
        _cells = [[NSMutableArray alloc] init];
        _heightForHeader = -1;
        _heightForFooter = -1;
    }
    return self;
}

#pragma mark - Add Cell

- (void)addNormalCell:(UIImage *)image title:(NSString *)title {
    [self addNormalCell:image title:title detail:nil attrbutes:nil push:nil target:nil selector:nil require:YES];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title require:(BOOL)require {
    [self addNormalCell:image title:title detail:nil attrbutes:nil push:nil target:nil selector:nil require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title push:(Class)push {
    [self addNormalCell:image title:title detail:nil attrbutes:nil push:push target:nil selector:nil require:YES];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title push:(Class)push require:(BOOL)require {
    [self addNormalCell:image title:title detail:nil attrbutes:nil push:push target:nil selector:nil require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title target:(id)target selector:(SEL)selector {
    [self addNormalCell:image title:title detail:nil attrbutes:nil push:nil target:target selector:selector require:YES];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title target:(id)target selector:(SEL)selector require:(BOOL)require {
    [self addNormalCell:image title:title detail:nil attrbutes:nil push:nil target:target selector:selector require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title attributes:(NSArray *)attributes target:(id)target selector:(SEL)selector require:(BOOL)require {
    [self addNormalCell:image title:title detail:nil attrbutes:attributes push:nil target:target selector:selector require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail {
    [self addNormalCell:image title:title detail:detail attrbutes:nil push:nil target:nil selector:nil require:YES];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail require:(BOOL)require {
    [self addNormalCell:image title:title detail:detail attrbutes:nil push:nil target:nil selector:nil require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail push:(Class)push {
    [self addNormalCell:image title:title detail:detail attrbutes:nil push:push target:nil selector:nil require:YES];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail push:(Class)push require:(BOOL)require {
    [self addNormalCell:image title:title detail:detail attrbutes:nil push:push target:nil selector:nil require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail target:(id)target selector:(SEL)selector {
    [self addNormalCell:image title:title detail:detail attrbutes:nil push:nil target:target selector:selector require:YES];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail target:(id)target selector:(SEL)selector require:(BOOL)require {
    [self addNormalCell:image title:title detail:detail attrbutes:nil push:nil target:target selector:selector require:require];
}

- (void)addNormalCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail attrbutes:(NSArray *)attrbutes push:(Class)push target:(id)target selector:(SEL)selector require:(BOOL)require {
    if (require == NO) {
        return;
    }
    MGTableViewCell *cell = [[MGTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.imageView.image = image;
    cell.textLabel.textColor = self.option.cellTextLabelTextColor;
    cell.textLabel.font = self.option.cellTextLabelFont;
    cell.detailTextLabel.text = detail;
    cell.detailTextLabel.textColor = self.option.cellDetailTextLabelTextColor;
    cell.detailTextLabel.font = self.option.cellDetailTextLabelFont;
    cell.backgroundColor = self.option.cellBackgroundColor;
    cell.selectionStyle = (push || (target && selector)) ? UITableViewCellSelectionStyleDefault : UITableViewCellSelectionStyleNone;
    cell.onSelectActionPushClass = push;
    cell.onSelectActionTarget = target;
    cell.onSelectActionSelector = selector;
    
    NSString *titleText = title;
    UITableViewCellAccessoryType accessoryType = UITableViewCellAccessoryNone;
    if ([title hasSuffix:@" >"]) {
        titleText = [title substringToIndex:title.length-2];
        accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (self.option.cellAccessoryDisclosureIndicator) {
            cell.accessoryView = [[UIImageView alloc] initWithImage:self.option.cellAccessoryDisclosureIndicator];
        }
    }
    cell.accessoryType = accessoryType;
    cell.textLabel.text = titleText;
    if (attrbutes) {
        NSMutableAttributedString *attrbuteTitle = [[NSMutableAttributedString alloc] initWithString:titleText attributes:@{
            NSForegroundColorAttributeName  : cell.textLabel.textColor,
            NSFontAttributeName             : cell.textLabel.font,
        }];
        for (NSDictionary *attrbute in attrbutes) {
            [attrbuteTitle addAttributes:attrbute[@"Attributes"] range:[attrbute[@"Range"] rangeValue]];
        }
        cell.textLabel.attributedText = attrbuteTitle;
    }
    
    if (self.option.cellConfiguration) {
        self.option.cellConfiguration(cell);
    }
    
    [self addCustomCell:cell];
}

- (void)addSwitchCell:(NSString *)title on:(BOOL)on target:(id)target selector:(SEL)selector {
    [self addSwitchCell:nil title:title detail:nil on:on target:target selector:selector];
}

- (void)addSwitchCell:(NSString *)title detail:(NSString *)detail on:(BOOL)on target:(id)target selector:(SEL)selector {
    [self addSwitchCell:nil title:title detail:detail on:on target:target selector:selector];
}

- (void)addSwitchCell:(UIImage *)image title:(NSString *)title detail:(NSString *)detail on:(BOOL)on target:(id)target selector:(SEL)selector {
    MGTableViewCell *cell = [[MGTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.imageView.image = image;
    cell.textLabel.text = title;
    cell.textLabel.textColor = self.option.cellTextLabelTextColor;
    cell.textLabel.font = self.option.cellTextLabelFont;
    cell.detailTextLabel.text = detail;
    cell.detailTextLabel.textColor = self.option.cellDetailTextLabelTextColor;
    cell.detailTextLabel.font = self.option.cellDetailTextLabelFont;
    cell.backgroundColor = self.option.cellBackgroundColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UISwitch *swc = [[UISwitch alloc] init];
    swc.on = on;
    swc.onTintColor = self.option.cellSwitchOnTintColor;
    cell.accessoryView = swc;
    if (detail) {
        cell.cellHeight = self.option.cellHeight+15;
    }
    
    if (self.option.cellConfiguration) {
        self.option.cellConfiguration(cell);
    }
    
    [self addCustomCell:cell];
}

- (void)addCustomCell:(MGTableViewCell *)cell {
    _cells = [self.cells arrayByAddingObject:cell];
}

@end
